﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MovieApp.Models;

namespace MovieApp.Controllers
{
    public class HomeController : Controller
    {

        private MoviesDBEntities _db = new MoviesDBEntities();
        
        //
        // GET: /Home/

        public ActionResult Index(string searchString)
        {

            var movies = from m in _db.Movies1 select m;
            
            if (!String.IsNullOrEmpty(searchString))
            {
                movies = movies.Where(s => s.Title.ToUpper().Contains(searchString.ToUpper()));
            }
            
            return View(movies.ToList()); //.ToList()
        }

        //
        // GET: /Home/Details/5

        public ActionResult Details(int id)
        {

            var movieToView= (from m in _db.Movies1
                               where m.Id == id
                               select m).First();

            return View(movieToView);
            
        }

        //
        // GET: /Home/Create

        public ActionResult Create()
        {
            Movie m = new Movie();
            return View(m);
        }

        //
        // GET: /Home/About
        //
        public ActionResult About()
        {
            return View();
        }

        //
        // POST: /Home/Create
        [HttpPost]
        public ActionResult Create([Bind(Exclude = "Id")] Movie movieToCreate)
        {

            if (!ModelState.IsValid)
            {
                return View(movieToCreate);
            }

            _db.AddToMovies1(movieToCreate);
            _db.SaveChanges();

            return RedirectToAction("Index");

        }


        
        //
        // GET: /Home/Edit/5
 
        public ActionResult Edit(int id)
        {
            var movieToEdit = (from m in _db.Movies1
                               where m.Id == id
                               select m).First();

            return View(movieToEdit);
        }

        //
        // POST: /Home/Edit/5

        [HttpPost]
        public ActionResult Edit(Movie movieToEdit)
        {

            var originalMovie = (from m in _db.Movies1
                                 where m.Id == movieToEdit.Id
                                 select m).First();

            if (!ModelState.IsValid)
            {
                return View(originalMovie);
            }

            _db.ApplyCurrentValues(originalMovie.EntityKey.EntitySetName, movieToEdit);
            _db.SaveChanges();

            return RedirectToAction("Index");

        } 


        //
        // GET: /Home/Delete/5
 
        public ActionResult Delete(int id)
        {
            var movieToDelete = (from m in _db.Movies1 
                                 where m.Id == id 
                                 select m).First();

            return View(movieToDelete);


        }

        //
        // POST: /Home/Delete/5

        [HttpPost]
        public ActionResult Delete(Models.Movie movieToDelete)
        {

            //The model state is invalid when this method 
            //is called as only the ID is set in the movieToDelete,
            //title is required by the Movie_Validation class
            //Remove the title key from the model state so that the
            //state is valid without this item
            ModelState.Remove("Title");

            var SelMovie = (from m in _db.Movies1 
                            where m.Id == movieToDelete.Id 
                            select m).First();

            if (!ModelState.IsValid)
            {
                return View(SelMovie);
            }

            _db.DeleteObject(SelMovie);
            _db.SaveChanges();

            return RedirectToAction("Index");

        }


    }
}
